package com.lucky.blog.controller;
import	java.time.Clock;

import com.lucky.blog.domain.ArticleDO;
import com.lucky.blog.domain.ContentDO;
import com.lucky.blog.service.ContentService;
import com.lucky.common.config.SystemConfig;
import com.lucky.common.domain.Tree;
import com.lucky.common.utils.*;
import com.lucky.system.domain.*;
import com.lucky.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.Context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by Administrator
 * 2018/7/24
 * 15:55
 **/
@RestController
@RequestMapping("/blog")
public class HomeController {

    @Autowired
    private SystemConfig config;
    @Autowired
    private MenuService menuService;

    @Autowired
    ContentService bContentService;

    @Autowired
    LinksService linksService;

    @Autowired
    TagService tagService;

    @Autowired
    NoticeService noticeService;

    @Autowired
    private SlideAdService slideAdService;

    private final static Logger log = LoggerFactory.getLogger(HomeController.class);


    @GetMapping("/generation")
    public R generation(){
        Map<String, Object> recommendMap = new HashMap<>();
        recommendMap.put("type", 1);
        recommendMap.put("offset", 0);
        recommendMap.put("limit", 2);
        Map<String, Object> contentMap = new HashMap<>();
        recommendMap.put("sort", "cid");
        recommendMap.put("offset", 0);
        recommendMap.put("limit", 10);
        try {
            Context context = new Context();
            context.setVariable("url", config.getUrl());
            context.setVariable("adList", slideAdService.list(new HashMap<>()));
            context.setVariable("recommendList", bContentService.list(recommendMap));
            context.setVariable("contentList", bContentService.homeList(contentMap));
            FileUtil.generateFile(context,"index", config.getGeneration(),"index.html");
        }catch (Exception e){
            log.error(e.getMessage());
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        if(config.isPush()){
            bContentService.execCMD();
        }
        return R.ok("生成成功");
    }


    @GetMapping("/getMenu")
    public List<Tree<MenuDO>> getMenu(){
        return menuService.getNav();
    }

    @GetMapping("/list")
    public PageUtils opentList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<ArticleDO> bContentList = bContentService.homeList(query);
        int total = bContentService.count(query);
        double pageCount =  Math.floor(total / query.getLimit()) + 1;
        return new PageUtils(total,(int) pageCount,query.getOffset(),bContentList);
    }

    @GetMapping("/tag/list")
    public PageUtils tagList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<TagDO> list = tagService.list(query);
        int total = tagService.count(query);
        return new PageUtils(list, total);
    }

    @GetMapping("/link/list")
    public PageUtils linkList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<LinksDO> list = linksService.list(query);
        int total = linksService.count(query);
        return new PageUtils(list, total);
    }

    @GetMapping("/get/article")
    public R detail(@RequestParam long cid) {
        ContentDO contentDO  = bContentService.get(cid);
        return R.ok().put("contentDO",contentDO);
    }

    @GetMapping("/notice")
    public List<NoticeDO> detail(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        return noticeService.list(query);
    }

    @GetMapping("/ad")
    public List<SlideAdDO> ad(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        return slideAdService.list(query);
    }




}
